package iit.projects.etaxi.model.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import iit.projects.etaxi.model.dto.CustomerSCDO;
import iit.projects.etaxi.model.entity.Customer;

public class HibernateCustomerDao extends HibernateDaoSupport implements CustomerDao {
	
	final Logger log = LoggerFactory.getLogger(HibernateCityDao.class);

	@Override
	public void addCustomer(Customer customer) {
		
		if(log.isDebugEnabled()){
			log.debug("Going to save Customer " + customer.getFirstName());
		}
		
		Session session = getSession(false);
		try{
			session.save(customer);
		}catch(HibernateException ex){
			throw convertHibernateAccessException(ex);
		}
	}
	
	@Override
	public Customer updateCustomer(Customer customer){
		if(log.isDebugEnabled()){
			log.debug("Going to update Customer " + customer.getId());
		}
		
		Session session = getSession(false);
		try{
			return (Customer)session.merge(customer);
		}catch(HibernateException ex){
			throw convertHibernateAccessException(ex);
		}
	}

	@Override
	public Customer findByUserName(String userName) {

		List<?> list = getHibernateTemplate().find("from Customer o where o.userName = ?", userName);
		if(list != null && !list.isEmpty()){
			return (Customer)list.iterator().next();
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Customer> findCustomers(CustomerSCDO customerSCDO) {
		if(log.isDebugEnabled()){
			log.debug("Finding Customers");
		}
		
		List<Customer> list = null;
		Session session = getSession(false);
		
		try {
			String firstName = customerSCDO.getFirstName();
			String mobile = customerSCDO.getMobileNumber();
			String customerCode = customerSCDO.getCustomerCode();

			Criteria criteria = session.createCriteria(Customer.class);
			
			if (customerCode != null && !customerCode.isEmpty()) {
				if (!customerCode.contains("%")) {
					customerCode = "%" + customerCode + "%";
				}
				criteria.add(Restrictions.like("customerCode", customerCode));
			}
			
			if (firstName != null && !firstName.isEmpty()) {
				if (!firstName.contains("%")) {
					firstName = "%" + firstName + "%";
				}
				criteria.add(Restrictions.like("firstName", firstName));
			}
			
			if (mobile != null && !mobile.isEmpty()) {
				if (!mobile.contains("%")) {
					mobile = "%" + mobile + "%";
				}
				criteria.add(Restrictions.like("mobileNumber", mobile));
			}

			criteria.addOrder(Order.asc("customerCode"));
			list = criteria.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return list;
	}
	
	@Override
	public void deleteCustomer(Customer customer){
		if (log.isDebugEnabled()) {
			log.debug("Deleting Customer " + customer.getUserName());
		}
		Session session = getSession(false);
		try {
			session.delete(customer);
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
	}



}
